/**
 * @ Author 何涛
 * @ Date 2016/02/19 01:09:14
 */

gpNamespace.SettingLayer = cc.Layer.extend({
	self: null,

	soundEffectSlider: null,
	musicSlider: null,

	ctor: function () {
		this._super();

		this.self = this;

		this.init();
	},

	init: function () {
		cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdksetting.Pdksetting_plist);

		var y1 = 340;
		var y2 = 200;
		//读取本地音量记录
		var effect = cc.sys.localStorage.getItem(gpNamespace.EFFECTSOUND);
		if (!(effect == null || effect == "")) {
			gpNamespace.SoundManager.setCurrentEffectVolumn(parseInt(effect));
			gpNamespace.SoundManager.setLastEffectVolumn();
		}
		var music = cc.sys.localStorage.getItem(gpNamespace.MUSICSOUND);
		if (!(music == null || music == "")) {
			gpNamespace.SoundManager.setCurrentMusicVolumn(parseInt(music));
			gpNamespace.SoundManager.setLastMusicVolumn();
		}

//		cc.log("effect:" + effect + "- music:" + music);

		var background = new cc.Sprite(gpNamespace.sz.Sz_Bg);
		background.attr({
			x: 202 + background.width / 2,
			y: 72 + background.height / 2,
			anchorX: 0.5,
			anchorY: 0.5,
		});
		this.addChild(background);

		//音效sliderbar
		var soundEffectSlider = new ccui.Slider();
		soundEffectSlider.setTouchEnabled(true);
		soundEffectSlider.loadBarTexture(gpNamespace.sz.Sz_VolBg, ccui.Widget.PLIST_TEXTURE);
		soundEffectSlider.loadSlidBallTextures(gpNamespace.sz.Sz_VolCtrl, gpNamespace.sz.Sz_VolCtrl, "", ccui.Widget.PLIST_TEXTURE);
		soundEffectSlider.loadProgressBarTexture(gpNamespace.sz.Sz_FillVol, ccui.Widget.PLIST_TEXTURE);
		soundEffectSlider.attr({
			x: 200 + soundEffectSlider.getContentSize().width / 2 + 47,
			y: y1 + soundEffectSlider.getContentSize().height / 2 -3
		});
		soundEffectSlider.addEventListener(this.soundSliderEvent, this);
		background.addChild(soundEffectSlider);

		var effectSound = gpNamespace.GameConfig.EFFECTSOUND;
		soundEffectSlider.setPercent(parseInt(effectSound));
		this.soundEffectSlider = soundEffectSlider;

		//音效开关
		var soundOffItem = new cc.MenuItemImage(gpNamespace.sz.Sz_VolMin, gpNamespace.sz.Sz_VolMin, null, this);
		soundOffItem.attr({
			x: -10,
			y: 33 / 2
		});
		var soundOffItem2 = cc.MenuItemSprite(new cc.Sprite(), new cc.Sprite(), this.soundOffCallFunction, this)
		soundOffItem2.setContentSize(soundOffItem.width*1.5, soundOffItem.height*1.5)
		soundOffItem2.attr({
			x: -20,
			y: 33 / 2
		});
		var soundOffMenu = new cc.Menu(soundOffItem,soundOffItem2);
		soundOffMenu.attr({
			x: 200,
			y: y1
		});
		background.addChild(soundOffMenu);

		var soundOnItem = new cc.MenuItemImage(gpNamespace.sz.Sz_VolMax, gpNamespace.sz.Sz_VolMax, null, this);
		soundOnItem.attr({
			x: 39 / 2,
			y: 33 / 2
		});
		var soundOnItem2 = cc.MenuItemSprite(new cc.Sprite(), new cc.Sprite(), this.soundOnCallFunction, this)
		soundOnItem2.setContentSize(soundOnItem.width*1.5, soundOnItem.height*1.5)
		soundOnItem2.attr({
			x: 39 / 2 + 10,
			y: 33 / 2
		});
		var soundOnMenu = new cc.Menu(soundOnItem,soundOnItem2);
		soundOnMenu.attr({
			x: 780,
			y: y1
		});
		background.addChild(soundOnMenu);

		//音乐sliderbar
		var musicSlider = new ccui.Slider();
		musicSlider.setTouchEnabled(true);
		musicSlider.loadBarTexture(gpNamespace.sz.Sz_VolBg, ccui.Widget.PLIST_TEXTURE);
		musicSlider.loadSlidBallTextures(gpNamespace.sz.Sz_VolCtrl, gpNamespace.sz.Sz_VolCtrl, "", ccui.Widget.PLIST_TEXTURE);
		musicSlider.loadProgressBarTexture(gpNamespace.sz.Sz_FillVol, ccui.Widget.PLIST_TEXTURE);
		musicSlider.attr({
			x: 200 + musicSlider.getContentSize().width / 2 + 47,
			y: y2 + musicSlider.getContentSize().height / 2
		});
		musicSlider.addEventListener(this.musicSliderEvent, this);
		background.addChild(musicSlider);

		var musicSound = gpNamespace.GameConfig.MUSICSOUND;
		musicSlider.setPercent(parseInt(musicSound));
		this.musicSlider = musicSlider;

		//音乐开关
		var musicOffItem = new cc.MenuItemImage(gpNamespace.sz.Sz_VolMin, gpNamespace.sz.Sz_VolMin, null, this);
		musicOffItem.attr({
			x: -10,
			y: 33 / 2
		});
		var musicOnItem2 = cc.MenuItemSprite(new cc.Sprite(), new cc.Sprite(), this.musicOffCallFunction, this)
		musicOnItem2.setContentSize(musicOffItem.width*1.5, musicOffItem.height*1.5)
		musicOnItem2.attr({
			x: -20,
			y: 33 / 2
		});
		var musicOffMenu = new cc.Menu(musicOffItem,musicOnItem2);
		musicOffMenu.attr({
			x: 200,
			y: y2
		});
		background.addChild(musicOffMenu);

		var musicOnItem = new cc.MenuItemImage(gpNamespace.sz.Sz_VolMax, gpNamespace.sz.Sz_VolMax, null, this);
		musicOnItem.attr({
			x: 39 / 2,
			y: 33 / 2
		});
		var musicOnItem2 = cc.MenuItemSprite(new cc.Sprite(), new cc.Sprite(), this.musicOnCallFunction, this)
		musicOnItem2.setContentSize(musicOnItem.width*1.5, musicOnItem.height*1.5)
		musicOnItem2.attr({
			x: 39 / 2 + 10,
			y: 33 / 2
		});
		var musicOnMenu = new cc.Menu(musicOnItem,musicOnItem2);
		musicOnMenu.attr({
			x: 780,
			y: y2
		});
		background.addChild(musicOnMenu);
	},

	soundSliderEvent: function (sender, type) {
		switch (type) {
		case ccui.Slider.EVENT_PERCENT_CHANGED:
			var percent = sender.getPercent();
			//设置音效音量
			gpNamespace.SoundManager.setEffectVolumn(percent);
			gpNamespace.GameConfig.EFFECTSOUND = percent;
//			cc.sys.localStorage.setItem(gpNamespace.EFFECTSOUND, percent);
			cc.log("###soundSliderEvent-percent:"+percent);
			break;
		default:
			break;
		}
	},

	musicSliderEvent: function (sender, type) {
		switch (type) {
		case ccui.Slider.EVENT_PERCENT_CHANGED:
			var percent = sender.getPercent();
			//设置音乐音量
			gpNamespace.SoundManager.setMusicVolume(percent);
			gpNamespace.GameConfig.MUSICSOUND = percent;
//			cc.sys.localStorage.setItem(gpNamespace.MUSICSOUND, percent);
			cc.log("###musicSliderEvent-percent:"+percent);
			break;
		default:
			break;
		}
	},

	soundOffCallFunction: function (sender) {
		cc.log("音效为0");

		tyNamespace.setMenuEnable(sender);

//		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);

		var self = this.self;
		var soundEffectSlider = self.soundEffectSlider;
		soundEffectSlider.setPercent(0);
		gpNamespace.SoundManager.minEffectVolumn();
	},

	soundOnCallFunction: function (sender) {
		cc.log("音效为1");

		tyNamespace.setMenuEnable(sender);

		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);

		var self = this.self;
		var soundEffectSlider = self.soundEffectSlider;
		soundEffectSlider.setPercent(100);
		gpNamespace.SoundManager.maxEffectVolumn();
	},

	musicOffCallFunction: function (sender) {
		cc.log("音乐为0");
		tyNamespace.setMenuEnable(sender);
//		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);
		var self = this.self;
		var musicSlider = self.musicSlider;
		musicSlider.setPercent(0);
		gpNamespace.SoundManager.minMusicVolumn();
	},

	musicOnCallFunction: function (sender) {
		cc.log("音乐为1");
		tyNamespace.setMenuEnable(sender);
		gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);
		var self = this.self;
		var musicSlider = self.musicSlider;
		musicSlider.setPercent(100);
		gpNamespace.SoundManager.maxMusicVolumn();
	},
	onExit:function(){
		this._super();
//		cc.log("onExit");
		cc.sys.localStorage.setItem(gpNamespace.EFFECTSOUND, gpNamespace.GameConfig.EFFECTSOUND);
		cc.sys.localStorage.setItem(gpNamespace.MUSICSOUND, gpNamespace.GameConfig.MUSICSOUND);
	}

});